# Makefile for compiling Nameservice

# Some paths
UTILSDIR=../../../utils
BASEDIR=../..
PROTOCOLDIR=../../protocol
GIMNETAP_COMMON_LIBDIR=../../commonlib
GIMNETAP_SERVICES_COMMON_LIBDIR=../common

# Some tools
CC=gcc

CFLAGS=-O2 -g3 -Wall -I. -I$(BASEDIR) -I$(PROTOCOLDIR) -I$(UTILSDIR)	\
-I$(BASEDIR)/services/common -I$(GIMNETAP_COMMON_LIBDIR) -DLINUX_OS		\
-DUSE_SELECT_IO $(CE)

LD=gcc
LDFLAGS=
LIBS=-lpthread -lrt -lssl -lcrypto

# Base Sources
SRCSBASE=$(UTILSDIR)/ownutils.c $(UTILSDIR)/owndebug.c $(UTILSDIR)/crc16.c \
				 $(PROTOCOLDIR)/protocolID.c 

# CommonLib
include $(GIMNETAP_COMMON_LIBDIR)/GIMnetAP-common.Makefile

# All sources
SRCS=$(SRCSBASE) $(GIMNETAP_COMMON_SRCS) $(GIMNETAP_SERVICES_COMMON_LIBDIR)/apservicebase.c

# Construct lists of CPP and C objects
OBJS=$(SRCS:.c=.o)

# Define COMPILE and LINK commands
COMPILE=$(CC) $(CFLAGS) -c
LINK=$(LD) $(LDFLAGS) 

all: nameservice nameservice_test

####### Nameservice target
NAMESERVICE_OBJECTS=$(OBJS) nameservice.o nameservicecore.o nametable.o
nameservice: $(OBJS) $(NAMESERVICE_OBJECTS)
	@$(LINK) -o $@ $(NAMESERVICE_OBJECTS) $(LIBS)
	@echo "      [LD] $@"

####### Nameservice testclient
NAMESERVICE_TEST_OBJECTS=$(OBJS) nameservice_test.o
nameservice_test: $(OBJS) $(NAMESERVICE_TEST_OBJECTS)
	@$(LINK) -o $@ $(NAMESERVICE_TEST_OBJECTS) $(LIBS) -lcurses
	@echo "      [LD] $@"

####### Special targets
nametable.unit: $(BASEDIR)/idmap.o \
								$(BASEDIR)/strmap.o \
								$(UTILSDIR)/ownutils.o \
	              $(UTILSDIR)/owndebug.o
	@$(COMPILE) -DCOMPILE_UNIT_TEST nametable.c -o nametable.o
	@$(LINK) $^ nametable.o -o nametable.unit $(LIBS)


.PHONY: common_clean
common_clean:
	@rm -f $(BASEDIR)/services/common/*.o
	@echo "      [CLEAN] common"


clean: common_clean
	@rm -f $(OBJS) *.o nameservice nameservice_test
	@echo "      [CLEAN] nameservice"

# Default compile rule.
%.o:	%.c
	@$(COMPILE) -o $@ $<
	@echo "      [CC] $<"

.PHONY: all clean depend testclients
